﻿#include "unrealircd.h"

typedef struct PhoneSMS PhoneSMS;

CMD_FUNC(cmd_phoneVerification);
void testSendSMS(Client *client ,char * phoneNumber,int sendType);

ModuleHeader MOD_HEADER
= {
	"third/sendPhoneSMS",
	"1.1",
	"This module will send phone message ",
	"waitch",
	"unrealircd-5",
};

MOD_INIT()
{
	
	CommandAdd(modinfo->handle, "PHONEVERIFICATION", cmd_phoneVerification, 2, CMD_UNREGISTERED|CMD_UNLOGIN|CMD_USER);
	

	return MOD_SUCCESS;
}

MOD_LOAD()
{
	return MOD_SUCCESS;
}

MOD_UNLOAD()
{
	return MOD_SUCCESS;
}


/**
 * 发送手机验证码的命令
 * parv[1]:手机号码
 * parv[2]:验证领域
 */
CMD_FUNC(cmd_phoneVerification){
	if (!IsAPPIdent(client)) {
		sendnumeric(client, ERR_WHITHOUTAPPIDENT);
		return;
	}
	if (parc != 3 || BadPtr(parv[2])) {
		sendnumeric(client, ERR_NEEDMOREPARAMS, "PHONEVERIFICATION");
		return;
	}
	if(!is_digist_string(parv[1],strlen(parv[1]))||strlen(parv[1])>20){
		sendnumeric(client, PARAMERROR, "Phone number","not right value");
		return;
	}
	if(!is_digist_string(parv[2],strlen(parv[2]))){
		sendnumeric(client, PARAMERROR, "smsStatus","not right value");
		return;
	}
	if(PhoneSms==NULL){
		sendnumeric(client, ERR_NEEDMOREPARAMS, "PHONEVERIFICATION not enable");
		return;
	}
	int smsStatus=atoi(parv[2]);
	ircd_log(LOG_ERROR,"smsStatus:%d",smsStatus);
	int type=0;

	char * phoneNumber=parv[1];
	//验证手机是否已经注册过了
	int telnum=0;
	D_User *users=queryImUserByTel(phoneNumber,&telnum);
	db_D_UserFree(users,telnum);
	switch(smsStatus){
		case SMS_STATUS_REGISTER:
			type=SMS_STATUS_REGISTER;
			if(telnum!=0){
				sendnumeric(client, PHONENUMBEREXITED, phoneNumber);
				return;
			}
			break;
		case SMS_STATUS_LOGIN:
			type=SMS_STATUS_LOGIN;
			if(telnum!=1){
				sendnumeric(client, PHONENUMBERNOTEXITED, phoneNumber);
				return;
			}
			break;
		case SMS_STATUS_DELETE:
			type=SMS_STATUS_DELETE;
			if(telnum!=1){
				sendnumeric(client, PHONENUMBERNOTEXITED, phoneNumber);
				return;
			}
			break;
		case SMS_STATUS_RESET_PASSWORD:
			type=SMS_STATUS_RESET_PASSWORD;
			if(telnum!=1){
				sendnumeric(client, PHONENUMBERNOTEXITED, phoneNumber);
				return;
			}
			break;
		default:
			sendnumeric(client, PARAMERROR, "smsStatus","not right value");
			return;
	}
	#ifndef DEBUG
		PhoneSms(client,phoneNumber,type);
	#else
		testSendSMS(client,phoneNumber,type);
	#endif
}

void testSendSMS(Client *client ,char * phoneNumber,int sendType){
	D_im_sms *sms_data=db_createImSMS();
	safe_strdup(sms_data->code,"111111");
	safe_strdup(sms_data->contactvalue,phoneNumber);
	sms_data->sms_channel=safe_alloc(sizeof(int));
	*(sms_data->sms_channel)=SMS_MODE_PHONE;
	sms_data->sms_type=safe_alloc(sizeof(int));
	*(sms_data->sms_type)=sendType;
	sms_data->used=safe_alloc(sizeof(int));
	*(sms_data->used)=0;
	sms_data->valid_time=safe_alloc(sizeof(D_TimeStamp));
	D_getCurrectTime(sms_data->valid_time);
	int insertStatus=0;
	char insert_error_message[200]="";
	D_im_sms *sms_in=insertImSms(sms_data,&insertStatus,insert_error_message);
	db_D_ImSMSFree(sms_data,1);
	db_D_ImSMSFree(sms_in,insertStatus);
	if(insertStatus==-1){
		ircd_log(LOG_ERROR, "insert im_sms error:%s", insert_error_message);
		return;
	}
	sendnumeric(client, SMSSENDSUCCESS, phoneNumber);
}